package com.zs.hydrant.controller;

import com.zs.hydrant.entity.FireHydrant;
import com.zs.hydrant.service.FireHydrantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hydrant")
public class FireHydrantController {
    @Autowired
    private FireHydrantService service;

    /**
     * 获取用户附近最近的消防栓
     * @param lat 用户纬度
     * @param lon 用户经度
     * @return 消防栓信息
     */
    @GetMapping("/nearest")
    public Map<String, Object> getNearestHydrant(
            @RequestParam BigDecimal lat,
            @RequestParam BigDecimal lon) {
        Map<String, Object> result = new HashMap<>();
        try {
            FireHydrant hydrant = service.getNearestHydrant(lat, lon);
            if (hydrant != null) {
                result.put("success", true);
                result.put("msg", "获取成功");
                result.put("data", hydrant);
            } else {
                result.put("success", false);
                result.put("msg", "未找到附近消防栓");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "获取消防栓信息失败：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 添加消防栓
     * @param hydrant 消防栓信息
     * @return 操作结果
     */
    @PostMapping("/add")
    public Map<String, Object> addHydrant(@RequestBody FireHydrant hydrant) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 设置默认采集人员ID为1（实际应该从登录用户获取）
            if (hydrant.getCollectorId() == null) {
                hydrant.setCollectorId(1);
            }
            
            int rows = service.addHydrant(hydrant);
            if (rows > 0) {
                result.put("success", true);
                result.put("msg", "消防栓添加成功");
            } else {
                result.put("success", false);
                result.put("msg", "消防栓添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "添加失败：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/all")
    public Map<String, Object> getAllHydrants() {
        Map<String, Object> result = new HashMap<>();
        try {
            List<FireHydrant> hydrantList = service.getAllHydrants();
            result.put("code", 200); // 新增code字段
            result.put("msg", "获取成功");
            result.put("data", hydrantList);
            result.put("total", hydrantList !=   null ? hydrantList.size() : 0);
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "获取失败：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @PostMapping("/delete")
    public String deleteHydrant(@RequestBody Map<String, Integer> params) {
        service.removeById(params.get("id"));
        return "{\"code\":200,\"msg\":\"删除成功\"}";
    }

    @PostMapping("/updateStatus")
    public Map<String, Object> updateStatus(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            Integer id = (Integer) params.get("id");
            Integer status = (Integer) params.get("status");
            if (id != null && status != null) {
                service.updateHydrantStatus(id, status);
                result.put("code", 200);
                result.put("msg", "状态更新成功");
            } else {
                result.put("code", 400);
                result.put("msg", "参数错误：ID或状态不能为空");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "状态更新失败：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 更新消防栓信息
     * @param hydrant 消防栓对象
     * @return 操作结果
     */
    @PutMapping("/updateById")
    public Map<String, Object> updateById(@RequestBody FireHydrant hydrant) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (hydrant.getId() == null) {
                result.put("success", false);
                result.put("msg", "消防栓ID不能为空");
                return result;
            }
            service.updateById(hydrant);
            result.put("success", true);
            result.put("msg", "消防栓更新成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "更新失败：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
    
    /**
     * 根据ID删除消防栓
     * @param id 消防栓ID
     * @return 操作结果
     */
    @DeleteMapping("/removeById/{id}")
    public Map<String, Object> removeById(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            service.removeById(id);
            result.put("success", true);
            result.put("msg", "消防栓删除成功");
        } catch (Exception e) {
            result.put("success", false);
            result.put("msg", "删除失败：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

}